﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Interop;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using RPAVIEW.views.EditPage;
using System.Diagnostics;

namespace RPAVIEW.util {
    public class WindowUtil {
        static Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
        static Dictionary<IntPtr, List<int>> windowHotKeys = new Dictionary<IntPtr, List<int>>();
        public delegate void HotKeyCallBackHanlder();
        private static int hotKeyId = 10;

        public static EditHome GetCurrentEditHome() { 
            EditHome currentEd = (EditHome)WindowUtil.GetWindow<MainWindow>().Content;
            return currentEd;
        }

        public static T GetWindow<T>()where T : Window { 
            T win = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is T) as T;
            return win;
        }

        public static void MinSizeWindow(Window win) { 
            win.WindowState = WindowState.Minimized;
        }
        
        public static void RecoverWindow(Window win) { 
            win.WindowState = WindowState.Normal;
            win.Activate();
            win.Show();
            User32Utils.SetForegroundWindow(ElementUtil.getWindowPtr(win));
        }

        /// <summary>
        /// 消息处理
        /// </summary>
        private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){
            if (msg == (int)WinMessages.WM_HOTKEY){
                int id = wParam.ToInt32();
                if (keymap.TryGetValue(id, out var callback)){
                    callback();
                }
                handled = true;
            }
            return IntPtr.Zero;
        }

        /// <summary>
        /// 注销快捷键
        /// </summary>
        /// <param name="hWnd">持有快捷键窗口的句柄</param>
        /// <param name="callBack">回调函数</param>
        public static void UnRegist(IntPtr hWnd){
            if(windowHotKeys.TryGetValue(hWnd, out List<int> ids)) {
                foreach(int id in ids) { 
                    User32Utils.UnregisterHotKey(hWnd, id);
                }
            }
        }

      
        /// <summary>
        /// 注册快捷键
        /// </summary>
        /// <param name="window">持有快捷键窗口</param>
        /// <param name="fsModifiers">辅助键</param>
        /// <param name="key">快捷键</param>
        /// <param name="callBack">回调函数</param>
        /// <param name="keyId">未必标识</param>
        public static void Regist(Window window, HotkeyModifiers fsModifiers, int vk, HotKeyCallBackHanlder callBack){
            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            HwndSource hwndSource = HwndSource.FromHwnd(hwnd);
            hwndSource.AddHook(WndProc);
            int id = hotKeyId++;
            //需要吧wpf中定义的key枚举转换完win32对应的vk码具体码请查阅https://docs.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes
            //var vk = KeyInterop.VirtualKeyFromKey(key);
            if(windowHotKeys.TryGetValue(hwnd, out List<int> ids)) { 
                ids.Add(id);
            } else { 
                List<int> list = new List<int>();
                list.Add(id);
                windowHotKeys.Add(hwnd, list);
            }
            if (!User32Utils.RegisterHotKey(hwnd, id, (uint)fsModifiers, (uint)vk)) { 
                //获取错误码
                throw new RpaRuntimeException(SystemUtil.GetLastErrorMsg());
            }
            keymap[id] = callBack;
        }

        public static List<IntPtr> GetProcessWindows(){ 
            List<IntPtr> list = new List<IntPtr>();
            Process[] processes = Process.GetProcesses();
            foreach(Process p in processes) {
                if(p.MainWindowHandle != null && p.MainWindowHandle != IntPtr.Zero) { 
                    list.Add(p.MainWindowHandle);
                }
            }
            return list;
        }
    }
}
